# Contributor: Sergei Lukin <sergej.lukin@gmail.com>
# Contributor: Łukasz Jendrysik <scadu@yandex.com>
# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=vim
pkgver=9.1.0707
pkgrel=0
pkgdesc="Improved vi-style text editor"
url="https://www.vim.org/"
arch="all"
license="Vim"
options="!check"  # some tests are failing
makedepends="
	gtk+3.0-dev
	libx11-dev
	libxt-dev
	lua5.4-dev
	ncurses-dev
	python3-dev
	ruby-dev
	"
subpackages="
	$pkgname-doc
	${pkgname}diff
	$pkgname-tutor
	$pkgname-common
	gvim
	xxd
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/vim/vim/archive/refs/tags/v$pkgver.tar.gz
	vimrc
	"

# secfixes:
#   9.1.0707-r0:
#     - CVE-2024-43790
#     - CVE-2024-43802
#   9.1.0678-r0:
#     - CVE-2024-43374
#   9.1.0652-r0:
#     - CVE-2024-41957
#     - CVE-2024-41965
#   9.0.2127-r0:
#     - CVE-2023-48706
#   9.0.2112-r0:
#     - CVE-2023-48231
#   9.0.2073-r0:
#     - CVE-2023-5535
#   9.0.1994-r0:
#     - CVE-2023-5344
#   9.0.1888-r0:
#     - CVE-2023-4733
#     - CVE-2023-4734
#     - CVE-2023-4735
#     - CVE-2023-4736
#     - CVE-2023-4738
#     - CVE-2023-4750
#     - CVE-2023-4752
#     - CVE-2023-4781
#   9.0.1413-r0:
#     - CVE-2023-1264
#   9.0.1395-r0:
#     - CVE-2023-1127
#     - CVE-2023-1170
#     - CVE-2023-1175
#     - CVE-2023-1355
#   9.0.1251-r0:
#     - CVE-2023-0433
#     - CVE-2023-0512
#   9.0.1198-r0:
#     - CVE-2023-0288
#   9.0.1167-r0:
#     - CVE-2023-0049
#     - CVE-2023-0051
#     - CVE-2023-0054
#   9.0.0999-r0:
#     - CVE-2022-4141
#     - CVE-2022-4292
#     - CVE-2022-4293
#     - CVE-2022-47024
#   9.0.0815-r0:
#     - CVE-2022-3705
#   9.0.0636-r0:
#     - CVE-2022-3352
#   9.0.0598-r0:
#     - CVE-2022-3234
#     - CVE-2022-3235
#     - CVE-2022-3256
#     - CVE-2022-3278
#   9.0.0437-r0:
#     - CVE-2022-3134
#   9.0.0369-r0:
#     - CVE-2022-2980
#     - CVE-2022-2982
#     - CVE-2022-3016
#     - CVE-2022-3037
#     - CVE-2022-3099
#   9.0.0270-r0:
#     - CVE-2022-2923
#     - CVE-2022-2946
#   9.0.0224-r0:
#     - CVE-2022-2816
#     - CVE-2022-2817
#     - CVE-2022-2819
#   9.0.0050-r0:
#     - CVE-2022-2264
#     - CVE-2022-2284
#     - CVE-2022-2285
#     - CVE-2022-2286
#     - CVE-2022-2287
#     - CVE-2022-2288
#     - CVE-2022-2289
#     - CVE-2022-2304
#   8.2.5170-r0:
#     - CVE-2022-2124
#     - CVE-2022-2125
#     - CVE-2022-2126
#     - CVE-2022-2129
#   8.2.5055-r0:
#     - CVE-2022-1851
#     - CVE-2022-1886
#     - CVE-2022-1898
#   8.2.5000-r0:
#     - CVE-2022-1796
#   8.2.4969-r0:
#     - CVE-2022-1619
#     - CVE-2022-1620
#     - CVE-2022-1621
#     - CVE-2022-1629
#   8.2.4836-r0:
#     - CVE-2022-1381
#   8.2.4708-r0:
#     - CVE-2022-1154
#     - CVE-2022-1160
#   8.2.4619-r0:
#     - CVE-2022-0943
#   8.2.4542-r0:
#     - CVE-2022-0572
#     - CVE-2022-0629
#     - CVE-2022-0685
#     - CVE-2022-0696
#     - CVE-2022-0714
#     - CVE-2022-0729
#   8.2.4350-r0:
#     - CVE-2022-0359
#     - CVE-2022-0361
#     - CVE-2022-0368
#     - CVE-2022-0392
#     - CVE-2022-0393
#     - CVE-2022-0407
#     - CVE-2022-0408
#     - CVE-2022-0413
#     - CVE-2022-0417
#     - CVE-2022-0443
#   8.2.4173-r0:
#     - CVE-2021-4069
#     - CVE-2021-4136
#     - CVE-2021-4166
#     - CVE-2021-4173
#     - CVE-2021-4187
#     - CVE-2021-4192
#     - CVE-2021-4193
#     - CVE-2021-46059
#     - CVE-2022-0128
#     - CVE-2022-0156
#     - CVE-2022-0158
#     - CVE-2022-0213
#   8.2.3779-r0:
#     - CVE-2021-4019
#   8.2.3650-r0:
#     - CVE-2021-3927
#     - CVE-2021-3928
#     - CVE-2021-3968
#     - CVE-2021-3973
#     - CVE-2021-3974
#     - CVE-2021-3984
#   8.2.3567-r0:
#     - CVE-2021-3903
#   8.2.3500-r0:
#     - CVE-2021-3875
#   8.2.3437-r0:
#     - CVE-2021-3770
#     - CVE-2021-3778
#     - CVE-2021-3796
#   8.1.1365-r0:
#     - CVE-2019-12735
#   8.0.1521-r0:
#     - CVE-2017-6350
#     - CVE-2017-6349
#   8.0.0329-r0:
#     - CVE-2017-5953
#   8.0.0056-r0:
#     - CVE-2016-1248

prepare() {
	# Read vimrc from /etc/vim
	echo '#define SYS_VIMRC_FILE "/etc/vim/vimrc"' >> src/feature.h

	cp -r "$builddir" "$builddir"-gvim
	default_prepare
}

build() {
	export CFLAGS="$CFLAGS -flto=auto"
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--enable-luainterp=dynamic \
		--enable-python3interp=dynamic \
		--enable-rubyinterp=dynamic \
		--without-x \
		--disable-nls \
		--enable-acl \
		--enable-multibyte \
		--enable-gui=no \
		--with-lua-prefix=/usr/lua5.4 \
		--with-compiledby="Alpine Linux"
	make
	cd "$builddir"-gvim
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--enable-luainterp=dynamic \
		--enable-python3interp=dynamic \
		--enable-rubyinterp=dynamic \
		--with-x \
		--disable-nls \
		--enable-acl \
		--enable-multibyte \
		--enable-gui=gtk3 \
		--with-lua-prefix=/usr/lua5.4 \
		--with-compiledby="Alpine Linux"
	make
}

check() {
	make test
}

package() {
	depends="vim-common=$pkgver-r$pkgrel xxd=$pkgver-r$pkgrel"
	provider_priority=10
	replaces="gvim"

	make -j1 DESTDIR="$pkgdir" install

	install -Dm644 runtime/doc/uganda.txt \
		"$pkgdir/usr/share/licenses/$pkgname/LICENSE"
	install -Dm644 "$srcdir"/vimrc "$pkgdir"/etc/vim/vimrc

	# only relevant to gvim
	rm -r "$pkgdir"/usr/share/icons/
	rm -r "$pkgdir"/usr/share/applications/
}

common() {
	replaces="vim gvim"
	arch="noarch"
	amove etc/vim/vimrc
	amove usr/share/vim
}

gvim() {
	pkgdesc="advanced text editor, with GUI"
	depends="vim-common=$pkgver-r$pkgrel xxd=$pkgver-r$pkgrel"
	provides="vim=$pkgver-r$pkgrel"
	provider_priority=1
	replaces="vim"

	make -C "$builddir"-gvim -j1 DESTDIR="$subpkgdir" install

	rm "$subpkgdir"/usr/bin/vimdiff
	rm "$subpkgdir"/usr/bin/vimtutor
	rm -r "$subpkgdir"/usr/share/man
	rm -r "$subpkgdir"/usr/share/vim
	rm "$subpkgdir"/usr/bin/xxd
}

vimdiff() {
	pkgdesc="view file diffs in vim"
	depends="diffutils vim=$pkgver-r$pkgrel"
	replaces="vim gvim"
	arch="noarch"
	options="!tracedeps"
	provides="cmd:vimdiff=$pkgver-r$pkgrel"

	amove usr/bin/vimdiff
}

tutor() {
	pkgdesc="vim tutor and tutor files"
	depends="vim=$pkgver-r$pkgrel"
	replaces="gvim vim"
	arch="noarch"

	amove usr/bin/vimtutor
	amove usr/share/vim/vim*/tutor
}

xxd() {
	pkgdesc="tool to make (or reverse) a hex dump"
	depends=""
	replaces="gvim vim"

	amove usr/bin/xxd
}

sha512sums="
694d9b8d08cd9a980c4ea9c8d6510181f1978ee72125bcde02add255057f2f36d8b14f54180e5ad715d8a09911496f79993a612128bed9f2deff3578d4f98660  vim-9.1.0707.tar.gz
bb4ac5bd2862754e741e63d7b521fd9e0cb1ac19769b952672928fd26471e9486182cd8a46a7e6f25e5b87759106e642293534c5e90c3c3958308291acb26875  vimrc
"
